﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using OpenSocialPlugin.General;
using RssToolkit.Rss;

namespace OpenSocialPlugin.Providers
{
  public class Photopoints : BaseSite, ISocialSite
  {

    public Photopoints ()
    {
      _capabilities = new List<CapabilitiesEnum>();
      _capabilities.Add(CapabilitiesEnum.HasPhotos);
    }

    public List<OSP_Person> GetFriends(OSP_Person person)
    {
      throw new NotImplementedException();
    }

    public List<OSP_Album> GetAlbums(OSP_Person person)
    {
      List<OSP_Album> resp = new List<OSP_Album>();
      string site = BaseSite.GetWebData("http://www.photopoints.com/main/rss/default.aspx");
      string siteurl = "";
      try
      {
        Regex regexObj = new Regex("CategoryName\">(.*?)</span>.*?title=\"RSS Feed\" href=\"(.*?)\" target=\"_blank\">", RegexOptions.Singleline);
        Match matchResult = regexObj.Match(site);
        while (matchResult.Success)
        {
          string name = (matchResult.Groups[1].Value);
          siteurl = "http://www.photopoints.com" + (matchResult.Groups[2].Value);
          matchResult = matchResult.NextMatch();
          resp.Add(new OSP_Album() { Id = siteurl,Title=name});
        }
      }
      catch (ArgumentException ex)
      {
        // Syntax error in the regular expression
      }
      return resp;
    }

    public List<OSP_Photo> GetPhotos(OSP_Album album)
    {
      List<OSP_Photo> resp = new List<OSP_Photo>();
      
      RssDocument doc = RssDocument.Load(BaseSite.GetWebData(album.Id));
      foreach (var rssItem in doc.Channel.Items)
      {
        Regex regexObj = new Regex("thumbID=(.*?)&", RegexOptions.Singleline);
        string id = regexObj.Match(rssItem.Description).Groups[1].Value;
        string imgurl = string.Format("http://www.photopoints.com/main/photos/streamthumb.aspx?thumbID={0}&thumbSize=1000", id);
        resp.Add(new OSP_Photo()
                   {
                     Title = rssItem.Title,
                     AlbumId = album.Id,
                     Id = id,
                     Thumb =
                       string.Format(
                         "http://www.photopoints.com/main/photos/streamthumb.aspx?thumbID={0}&thumbSize=200", id),
                     Url =
                       string.Format(
                         "http://www.photopoints.com/main/photos/streamthumb.aspx?thumbID={0}&thumbSize=1000", id)
                   });
      }
      return resp;
    }

    public GeneralResponse Home()
    {
      GeneralResponse res = new GeneralResponse();
      res.Response = GetAlbums(new OSP_Person());
      return res;
    }

    public GeneralResponse Search(string serchtxt, int type)
    {
      throw new NotImplementedException();
    }

    public GeneralResponse Extras(string serchtxt, int type)
    {
      throw new NotImplementedException();
    }

    public void Init()
    {
      
    }

    public OSP_Person Login()
    {
      return new OSP_Person();
    }

    public Control SettingForm
    {
      get { throw new NotImplementedException(); }
    }

    List<CapabilitiesEnum> _capabilities;
    public List<CapabilitiesEnum> Capabilities { get { return _capabilities; } }

    public OSP_Person GetPerson(string personId)
    {
      throw new NotImplementedException();
    }

    public string GetPersonInfo(OSP_Person person)
    {
      throw new NotImplementedException();
    }

    public string GetPhotoInfo(OSP_Photo photo)
    {
      throw new NotImplementedException();
    }

    public List<string> SearchNames
    {
      get { throw new NotImplementedException(); }
    }

    public List<string> ExtrasNames
    {
      get { throw new NotImplementedException(); }
    }

    public override string ToString()
    {
      return "Photopoints";
    }
  }
}
